package com.hn658.groupshare.security.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hn658.groupshare.common.enums.VerifyCodeType;
import com.hn658.groupshare.common.utils.RandomNumUtils;
import com.hn658.groupshare.security.dao.IVerifyCodeRedisDao;
import com.hn658.groupshare.security.exception.VerifyException;
import com.hn658.groupshare.security.service.IVerifyCodeService;

/**
 * 
 * @author ztjie
 *
 */
@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {

	@Autowired
	private IVerifyCodeRedisDao verifyCodeRedisDao;
	
	@Override
	public String createAndSaveMobileVerifyCode(String phone, VerifyCodeType type){
		if(StringUtils.isBlank(phone)){
			throw new VerifyException(VerifyException.VerifyPhoneNotEmpty);
		}
		if(type==null){
			throw new VerifyException(VerifyException.VerifyTypeNotEmpty);
		}
		String code = RandomNumUtils.geneRandomNum(4);
		verifyCodeRedisDao.saveVerifyCode(phone, type, code, 60);
		return code;
	}
	
	@Override
	public String getMobileVerifyCode(String phone,VerifyCodeType type){
		if(StringUtils.isBlank(phone)){
			throw new VerifyException(VerifyException.VerifyPhoneNotEmpty);
		}
		if(type==null){
			throw new VerifyException(VerifyException.VerifyTypeNotEmpty);
		}
		return verifyCodeRedisDao.getVerifyCode(phone, type);
	}

    @Override
    public void delectMobileVerifyCode(String phone,VerifyCodeType type){
    	if(StringUtils.isBlank(phone)){
			throw new VerifyException(VerifyException.VerifyPhoneNotEmpty);
		}
		if(type==null){
			throw new VerifyException(VerifyException.VerifyTypeNotEmpty);
		}
         verifyCodeRedisDao.delectVerifyCode(phone,type);
    }
}
